package net.lzzy.practices.wed.course;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.lzzy.practices.constants.ApiConstants;
import net.lzzy.practices.constants.Messages;
import net.lzzy.practices.entities.Course;
import net.lzzy.practices.entities.User;
import net.lzzy.practices.services.CourseService;
import net.lzzy.practices.services.UserService;
import net.lzzy.practices.utils.StringUtils;
import net.lzzy.practices.wed.models.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Api(tags = "课程相关业务")
@CrossOrigin
@RestController
@RequestMapping(ApiConstants.ROUTE_COURSE_ROOT)
public class CourseApiController {
    private final CourseService service;
    private final UserService uService;

    @Autowired
    public CourseApiController(CourseService service,UserService uService) {
        this.service = service;
        this.uService = uService;
    }

    @ApiOperation(value = "获取所有课程", notes = "<b>参数列表：<b/><br>&nbsp;&nbsp;&nbsp;&nbsp;token值")
    @GetMapping(ApiConstants.ROUTE_COURSE_ALL)
    public ApiResult getCourses(@RequestParam String token){
        List<VmCourse> courses = new ArrayList<>();
        service.get().forEach(course -> courses.add(VmCourse.create(course,false)));
        return new ApiResult(true,courses.size() + "",courses);
    }

    @ApiOperation(value = "分页获取所有课程", notes = "<b>参数列表：<b/><br>&nbsp;&nbsp;&nbsp;&nbsp;token值")
    @GetMapping(ApiConstants.ROUTE_COURSE_OPEN)
    public ApiResult getOpenCourses(@RequestParam String token){
        List<VmCourse> courses = new ArrayList<>();
        service.get(true).forEach(course -> courses.add(VmCourse.create(course,false)));
        return new ApiResult(true,courses.size() + "",courses);
    }

    @ApiOperation(value = "获取所有开放课程", notes = "<b>参数列表：<b/><br>&nbsp;&nbsp;&nbsp;&nbsp;1.token(String)<br/>&nbsp;&nbsp;&nbsp;&nbsp;2.page(int)<br/>" +
            "&nbsp;&nbsp;&nbsp;&nbsp;3.size(int)<br>")
    @GetMapping(ApiConstants.ROUTE_COURSE_PAGE)
    public ApiResult getPageCourses(@RequestParam String token,@RequestParam int page,@RequestParam int size){
        List<VmCourse> courses = new ArrayList<>();
        service.get(page,size).forEach(course -> courses.add(VmCourse.create(course,false)));
        return new ApiResult(true,service.count() + "",courses);
    }

    @ApiOperation(value = "分页获取所有开放课程", notes = "<b>参数列表：<b/><br>&nbsp;&nbsp;&nbsp;&nbsp;1.token(String)<br/>&nbsp;&nbsp;&nbsp;&nbsp;2.page(int)<br/>" +
            "&nbsp;&nbsp;&nbsp;&nbsp;3.size(int)<br>")
    @GetMapping(ApiConstants.ROUTE_COURSE_OPEN_PAGE)
    public ApiResult getPageOpenCourses(@RequestParam String token,@RequestParam int page,@RequestParam int size){
        List<VmCourse> courses = new ArrayList<>();
        service.get().forEach(course -> courses.add(VmCourse.create(course,false)));
        return new ApiResult(true,service.count() + "",courses);
    }

    @ApiOperation(value = "获取某一教师所有课程", notes = "<b>参数列表：<b/><br>&nbsp;&nbsp;&nbsp;&nbsp;1.token(String)<br/>&nbsp;&nbsp;&nbsp;&nbsp;2.teacherId(UUID)<br/>")
    @GetMapping(ApiConstants.ROUTE_COURSE_TEACHER)
    public ApiResult getTeacherCourses(@RequestParam String token, @RequestParam (name = "id")UUID teacherId){
        List<VmCourse> courses = new ArrayList<>();
        service.getTeachersCourse(teacherId).forEach(course -> courses.add(VmCourse.create(course,false)));
        return new ApiResult(true,courses.size() + "",courses);
    }

    @ApiOperation(value = "获取某一学生所有课程", notes = "<b>参数列表：<b/><br>&nbsp;&nbsp;&nbsp;&nbsp;1.token(String)<br/>&nbsp;&nbsp;&nbsp;&nbsp;2.studentId(UUID)<br/>")
    @GetMapping(ApiConstants.ROUTE_COURSE_STUDENT)
    public ApiResult getStudentsCourses(@RequestParam String token, @RequestParam (name = "id")UUID studentId){
        List<VmCourse> courses = new ArrayList<>();
        service.getStudentsCourse(studentId).forEach(course -> courses.add(VmCourse.create(course,false)));
        return new ApiResult(true,courses.size() + "",courses);
    }

    @ApiOperation(value = "创建课程", notes = "<b>参数值：<b/><br/>&nbsp;&nbsp;&nbsp;&nbsp;info值<br/>&nbsp;&nbsp;&nbsp;&nbsp;1.token(String)<br/>" +
            "&nbsp;&nbsp;&nbsp;&nbsp;2.name(String)<br/>&nbsp;&nbsp;&nbsp;&nbsp;3.desc(String)<br/>&nbsp;&nbsp;&nbsp;&nbsp;4.cover(String)<br/>" +
            "&nbsp;&nbsp;&nbsp;&nbsp;5.teacherId(UUID)<br/>&nbsp;&nbsp;&nbsp;&nbsp;6.open(boolean)<br/>")
    @PostMapping(value = ApiConstants.ROUTE_COURSE_ADD,produces = "application/json",consumes = "application/json")
    public ApiResult add(@RequestBody CourseInfo info){
        return addOrUpdate(null,info);
    }


    @ApiOperation(value = "添加或更新课程的处理")
    private ApiResult addOrUpdate(UUID id,CourseInfo info){
        String token = info.getToken();
        String name = info.getName();
        String desc = info.getDesc();
        String cover = info.getCover();
        String teacherId = info.getTid();
        boolean open = info.isOpen();
        if(StringUtils.isEmpty(name)||
                StringUtils.isEmpty(token)||
                StringUtils.isEmpty(desc)||
                StringUtils.isEmpty(cover)||
                StringUtils.isEmpty(teacherId)){
            return new ApiResult(false, Messages.INCOMPLETE_INFO.toString(),null);
        }
        User teacher = uService.getById(UUID.fromString(teacherId));
        if (teacher == null){
            return new ApiResult(false, Messages.WRONG_ID.toString(),null);
        }
        Course course;
        boolean create = id == null;
        if (create){
            course = new Course();
            course.setTeacher(teacher);
        }else {
            course = service.getById(id);
            if (course == null){
                return new ApiResult(false, Messages.WRONG_ID.toString(), null);
            }
        }
        course.setOpen(open);
        course.setCover(cover);
        course.setName(name);
        course.setDescription(desc);
        if (create){
            return new ApiResult(true,Messages.SUCCESS.toString(),VmCourse.create(service.add(course),true));
        }else {
            Course updated = service.update(course,token);
            if (updated == null){
                return new ApiResult(false, Messages.NO_PERMISSION.toString(),null);
            }
            return new ApiResult(true,Messages.SUCCESS.toString(),VmCourse.create(updated,true));
        }
    }

    @ApiOperation(value = "更新课程", notes = "<b>参数值：<b/><br/>&nbsp;&nbsp;&nbsp;&nbsp;info值<br/>&nbsp;&nbsp;&nbsp;&nbsp;1.token(String)<br/>" +
            "&nbsp;&nbsp;&nbsp;&nbsp;2.name(String)<br/>&nbsp;&nbsp;&nbsp;&nbsp;3.desc(String)<br/>&nbsp;&nbsp;&nbsp;&nbsp;4.cover(String)<br/>" +
            "&nbsp;&nbsp;&nbsp;&nbsp;5.teacherId(UUID)<br/>&nbsp;&nbsp;&nbsp;&nbsp;6.open(boolean)<br/>")
    @PostMapping(ApiConstants.ROUTE_COURSE_UPDATE)
    public ApiResult update(@PathVariable UUID id,@RequestBody CourseInfo info){
        return addOrUpdate(id,info);
    }

    @ApiOperation(value = "开放关闭课程", notes = "<b>参数列表：<b/><br/>&nbsp;&nbsp;&nbsp;&nbsp;1.token(String)<br/>&nbsp;&nbsp;&nbsp;&nbsp;2.strCourseId(String)<br/>" +
            "&nbsp;&nbsp;&nbsp;&nbsp;3.open(boolean)<br/>")
    @PostMapping(ApiConstants.ROUTE_COURSE_OPEN_CLOSE)
    public ApiResult openCloseCourse(@RequestBody CourseOpenInfo info){
        String strCourseId = info.getCourseId();
        String token = info.getToken();
        boolean open = info.isOpen();
        if (StringUtils.isEmpty(strCourseId) || StringUtils.isEmpty(token)){
            return new ApiResult(false, Messages.INCOMPLETE_INFO.toString(),null);
        }
        Course course = service.openCloseCourse(UUID.fromString(strCourseId),open,token);
        if (course == null){
            return new ApiResult(false, Messages.WRONG_ID.toString(),null);
        }
        return new ApiResult(true,Messages.SUCCESS.toString(),VmCourse.create(course,true));
    }

    @ApiOperation(value = "删除课程", notes = "<b>参数列表：<b/><br/>&nbsp;&nbsp;&nbsp;&nbsp;1.token(String)<br/>&nbsp;&nbsp;&nbsp;&nbsp;2.strCourseId(String)<br/>")
    @DeleteMapping(ApiConstants.ROUTE_COURSE_REMOVE)
    public ApiResult delete(@PathVariable CourseKeyInfo info){
        String strCourseId = info.getCourseId();
        String token = info.getToken();
        if (StringUtils.isEmpty(strCourseId) || StringUtils.isEmpty(token)){
            return new ApiResult(false, Messages.INCOMPLETE_INFO.toString(),null);
        }
        Messages msg = service.remove(UUID.fromString(strCourseId),null);
        return new ApiResult(msg == Messages.SUCCESS,msg.toString(),null);
    }
}
